
package com.borqs.music.tasks;

import static com.borqs.music.util.MusicConf.ReqType.MUSIC_SET_SETTINGS;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.http.RequestMethod;
import com.borqs.music.util.MusicConf;
import com.borqs.music.util.MusicConf.ServerSettingNames;

public class UpdateSettingsTask extends Task implements ServerSettingNames{
    public static final int PRIVACY_PRIVATE = 0;
    public static final int PRIVACY_FRIENDS = 1;
    public static final int PRIVACY_PUBLIC = 2;

    private String mTicket;
    private int mPrivacy;

    public UpdateSettingsTask(TaskListener listener, String ticket, int privacy) {
        super(listener);

        mTicket = ticket;
        mPrivacy = privacy;
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("name", PRIVACY);
        params.put("value", "" + mPrivacy);
        
        final String URL = MusicConf.getUrl(MUSIC_SET_SETTINGS);
        HttpRequest request = new HttpRequest(RequestMethod.GET, URL, mTicket, params);
        return request;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        JSONObject jObj = resp.asJSONObject();
        if(jObj.getBoolean("result")) {
            return mPrivacy;
        } else {
            throw new Exception("Set Failed:" + resp.asString());
        }
    }
}

